package com.hyy.lotterysystem.dao.mapper;

import com.hyy.lotterysystem.dao.dataobject.Encrypt;
import com.hyy.lotterysystem.dao.dataobject.UserDO;
import jakarta.validation.constraints.NotBlank;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select create_user_id from activity where activity_id=#{activityId}")
    Long getCreateActivityUserId(long activityId);

    /*查询邮箱绑定人数
    * @param email
    * @return*/
    @Select("select count(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);

    @Select("select count(*) from user where phone_number=#{phoneNumber}")
    int countByPhone(@NotBlank(message="电话不能为空") Encrypt phoneNumber);


    //keyProperty = "id"是Java对象的属性名字
    //keyColumn = "id"是数据库中属性的名字
    //Options可以把mysql中的属性赋值给Java对象中的属性
    //useGeneratedKeys = true是主键自增（数据库中）
    @Insert("insert into user (user_name,email,phone_number,password,identity)" +
            " values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(UserDO user);



    //查询email
    @Select("select * from user where email=#{email}")
    UserDO selectMail(@Param("email") String loginName);

    //查询电话
    @Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectPhone(@Param("phoneNumber")Encrypt phoneNumber);

    @Select("<script>" +
            " select * from user" +
            " <if test=\"identity!=null\">" +
            "    where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identityString);

    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);


    @Select("select * from user where email=#{email}")
    UserDO selectByEmail(String email);
}
